package org.qs.webservice.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.qs.webservice.dao.user.UsConsumeMapper;
import org.qs.webservice.domain.UsConsume;
import org.qs.webservice.service.IreadMemcacheService;
import org.qs.webservice.service.UserConsumeService;
import org.qs.webservice.util.DateUtil;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.qs.webservice.vo.Consume;
import org.qs.webservice.vo.PageData;
import org.springframework.stereotype.Service;



@Service
public class UserConsumeServiceImpl implements UserConsumeService {
	@Resource
	private IreadMemcacheService ireadMemcacheService;
	@Resource
	private  UsConsumeMapper usConsumeMapper;
	
	@Override
	public String getConsume(int content_id, int consume_type, String token,
			String device_identifier,int pages,int contents,String begin_time,String end_time) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		if(StringUtils.isNullOrEmpty(end_time)){
			end_time = DateUtil.getString(new Date(), "yyyy-MM-dd HH:mm:ss");
		}
		if(StringUtils.isNullOrEmpty(begin_time)){
			Calendar calendar = Calendar.getInstance(); 
			calendar.add(Calendar.MONTH, -1);    //得到前一个月 
		
			begin_time = DateUtil.getString(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
		}
		int index=(pages-1)*contents;
		PageData<Consume> page = new PageData<Consume>();
		String user_id=IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		
		if(!StringUtils.isNullOrEmpty(user_id)){
			List<UsConsume> usConsumes = usConsumeMapper.getUsConsumeListByParam(Long.valueOf(user_id), content_id, consume_type, begin_time, end_time, index, contents);
			List<Consume> consumes = new ArrayList<Consume>();
			for(UsConsume usConsume:usConsumes){
				Consume consume = new Consume();
				consume.setId(usConsume.getId()+"");
				consume.setConsume_fee(usConsume.getConsume_fee());
				if(usConsume.getConsume_type()==1){
					consume.setConsume_type("按章订购");
				}else if(usConsume.getConsume_type()==2){
					consume.setConsume_type("按本订购");
				}if(usConsume.getConsume_type()==3){
					consume.setConsume_type("打赏");
				}if(usConsume.getConsume_type()==4){
					consume.setConsume_type("月票");
				}
				consume.setContent_id(usConsume.getContent_id()+"");
				consume.setContent_name(usConsume.getContent_name());
				consume.setCreate_time(DateUtil.getString(usConsume.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
				consumes.add(consume);
			}
			page.setContent(consumes);
			page.setCode(200);
			page.setPageNumber(pages);
			page.setPageSize(contents);
			page.setTotal( usConsumeMapper.getUsConsumeListCountByParam(Long.valueOf(user_id), content_id, consume_type, begin_time, end_time));
			return IReadUtils.beanToJson(page); 
		}else{
			tokenMap.put("code", 403);
			tokenMap.put("reason", "用户未登陆");
			
		}
		return IReadUtils.beanToJson(tokenMap); 
	}

	
}
